<?php include_component('questionaire', 'tabs') ?>

<?php use_stylesheets_for_form($form) ?>
<?php use_javascripts_for_form($form) ?>

<div class="sf_admin_form">
    <table style="float:left">
        <tr>
            <td>
                <fieldset>
                    <legend class="formLabel"><?php echo __('Questions') ?></legend>
                    <table>
                        <?php foreach ($questions as $index => $question): ?>
                            <tr>
                                <td><?php echo $question->getName() ?></td>
                                <td style="width:100px;"></td>
                                <td>
                                    <?php if ($question->getAnswerType() == false): ?>
                                        <?php echo __('You need to set answer type in questions page') ?>
                                    <?php elseif ($question->getAnswerType() == 'text' ||
                                            $question->getAnswerType() == 'number'): ?>
                                        <?php echo __('Answer type is simple input') ?>                                                                        
                                    <?php elseif ($selQuestion && $selQuestion->getId() == $question->getId()): ?>                                      
                                        <form action="<?php echo url_for('@admin_page?module=questionaire&action=answers&id=' 
                                                . $qId . '&question_id=' . $selQuestion['id']) ?>" method="post" class="answers_form">
                                            <fieldset>
                                                <legend class="formLabel"><?php echo $question->getName().' '.__('answers') ?></legend>
                                                <table>
                                                    <?php foreach ($form['answers'] as $index => $subform): ?>
                                                        <?php echo $subform->renderHiddenFields(true) ?>
                                                        <tr>
                                                            <td style="width:80px;"><?php echo $subform['label']->renderLabel('Answer ' . ($index + 1), array('class' => 'formLabel')) ?> </td>
                                                            <td colspan="5"><?php echo $subform['label']->render(array('class' => 'formInput', 'style' => 'width:500px;')) ?> </td>
                                                            <td><?php echo $subform['label']->renderError() ?> </td>
                                                            <?php if ($index + 1 != count($form['answers'])): ?>
                                                                <td style="width:30px;">&nbsp;</td>
                                                                <td>
                                                                    <input id="<?php echo $subform['id']->getValue() ?>" class="formSubmit answer_delete" type="submit" name="delete" value="<?php echo __('Delete') ?>" />
                                                                </td>
                                                            <?php endif; ?>
                                                        </tr>
                                                    <?php endforeach; ?>
                                                    <tr><td>&nbsp;</td></tr>
                                                    <tr>
                                                        <td style="float:left;">
                                                            <input class="formSubmit save_answers" type="submit" name="save" value="<?php echo __('Save') ?>" />
                                                        </td>
                                                        <td></td>
                                                        <td style="float:right;">
                                                            <input class="formSubmit close_answers" type="submit" name="close" value="<?php echo __('Close') ?>" />
                                                        </td>
                                                    </tr>                                                            
                                                </table>
                                            </fieldset>          
                                        </form>
                                    <?php else: ?>
                                        <input id="<?php echo $question->getId() ?>" class="formSubmit addAnswers" type="submit" name="addAnswer" value="<?php echo __('Add answers') ?>" />                                        
                                    <?php endif; ?>
                                </td>                                  <td>                                    <?php if($answers[$question->getId()] > 0): ?>                                      <span style="color: green; text-weight: bold"><i>( <?php echo $answers[$question->getId()] ?> )</i></span>                                    <?php else: ?>                                      <span style="color: red; text-weight: bold"><i>( <?php echo $answers[$question->getId()] ?> )</i></span>                                    <?php endif ?>                                  </td>                                                              <td>                                 <?php if((strpos($question->getAnswerType(), 'select') !== false) || ($question->getAnswerType() == 'multinumber')): ?>                                          <?php echo __(ucfirst($question->getAnswerType())) ?>                                        <?php endif ?>                               </td>
                            </tr>
                        <?php endforeach; ?>
                    </table>
                </fieldset>
            </td>
        </tr>
    </table>
</div>

<?php include_component('questionaire', 'footerTabs') ?>

<script type="text/javascript">
<?php if ($selQuestion): ?>
            $('.answer_delete').click(function () {
                $.get('<?php echo url_for('@admin_page?module=questionaire&action=deleteAnswer&id=') ?>' + $(this).attr('id'), function(data) {
                    window.location.href = '<?php echo url_for('@admin_page?module=questionaire&action=answers&id=' . $qId . '&question_id=' . $selQuestion['id']) ?>';
                });
           
                return false;
            });    
<?php endif; ?>

        $('.close_answers').click(function() {
            window.location.href = '<?php echo url_for('@admin_page?module=questionaire&action=answers&id=' . $qId) ?>'; 
            
            return false;
        });
    
        $('.addAnswers').click(function() {
            window.location.href = '<?php echo url_for('@admin_page?module=questionaire&action=answers&id=' . $qId . '&question_id=') ?>' + $(this).attr('id');
            
            return false;
        });
</script>